$.browser = {};
$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
$.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());
$.extend($.fn, {
    serializeJSON: function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    },
    serializeTextJSON: function (option) {
        var $option = $.extend({
            key: function () {
                return $(this).attr('name');
            },
            value: function () {
                var select = $(this).find(':selected');
                if (select.val() == "")return "";
                return select.text();
            }
        }, option);
        var o = {};
        $(this).find('select').each(function () {
            o[$option.key.apply(this)] = $option.value.apply(this)
        });
        return o;
    },
    /**
     * 第一次绑定成功之后，缓存方法到元素的data属性里面。
     * 每次调用此方法删除之前singleUnAndBind绑定的事件，再绑定一次之前的事件
     * @param event 事件名称
     * @param fun 绑定事件
     * @returns {jQuery|HTMLElement}
     */
    singleUnAndBind: function (event, fun) {
        var event = {name: event, fun: fun};
        if (!!$(this).data('bindFirstEvent')) {
            event = $(this).data('bindFirstEvent');
        }
        $(this).unbind(event.name, event.fun).bind(event.name, event.fun);
        $(this).data('bindFirstEvent', event);
        return $(this);
    },
    refreshOpt: function (json, opt) {
        var setting = $.extend({
            resultFormat: {
                value: 'code',
                text: 'name'
            },
            render: '<option value="${value}">${text}</option>',
            defaultEmpty: [{}],
            deleteEmpty: true,
            add: function (data, $option) {

            }
        }, opt);
        //无默认为空的数组
        if (!setting.defaultEmpty[0][setting.resultFormat.text]) {
            //设置默认值
            setting.defaultEmpty[0][setting.resultFormat.value] = "";
            setting.defaultEmpty[0][setting.resultFormat.text] = "暂无数据";
        }
        //判断是否是报错页面
        if (!(json instanceof Array)) {
            return $(this);
        }
        //数据为空时显示空文案
        if ((!(json instanceof Array) || json.length == 0) && setting.deleteEmpty) {
            json = setting.defaultEmpty;
        }
        //是否删除空value的option
        if (setting.deleteEmpty) {
            $(this).find('option').remove();
        } else {
            $(this).find('option').not("[value='']").remove();
        }

        for (var i = 0; i < json.length; i++) {
            var $option = setting.render.replace('${value}', json[i][setting.resultFormat.value]);
            $option = $($option.replace('${text}', json[i][setting.resultFormat.text]));
            setting.add(json[i], $option);
            $(this).append($option);
        }
        $(this).select2();
        return $(this);
    },
    findSelf: function (sp) {
        var result = $(this).find(sp);
        if ($(this).is(sp)) {
            if (result.length == 0) result = $(this);
            else result.add($(this));
        }
        return result;
    }
});
$.location = {
    match: {
        protocol: /([^\/]+:)\/\/(.*)/i,
        host: /(^[^\:\/]+)((?:\/|:|$)?.*)/,
        port: /\:?([^\/]*)(\/?.*)/,
        pathname: /([^\?#]+)(\??[^#]*)(#?.*)/
    },
    parseUrl: function (url) {
        var tmp, res = {};
        res["href"] = url;
        for (var p in this.match) {
            tmp = this.match[p].exec(url);
            if (!tmp)continue;
            res[p] = tmp[1];
            url = tmp[2];
            if (url === "") {
                url = "/";
            }
            if (p === "pathname") {
                res["pathname"] = tmp[1];
                res["search"] = tmp[2];
                res["hash"] = tmp[3];
            }
        }
        return res;
    },
    param: function (key) {
        var url = location.search; //获取url中"?"符后的字串
        var request = {};
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                request[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
            }
        }
        return !!key ? request[key] : request;
    }
}
;